/*
 * Created by JFormDesigner on Sun Jun 25 14:33:10 CST 2023
 */

package org.scct.chenhui.frames;

import org.scct.chenhui.dao.UserDao;
import org.scct.chenhui.model.UserBean;
import org.scct.chenhui.utils.ActionDo;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

/**
 * @author Administrator
 */
public class DlgUpdateUser extends JDialog {
    ActionDo actionDo;
    int id;
    public DlgUpdateUser(Window owner, int id, ActionDo actionDo) {
        super(owner);
        initComponents();
        this.id=id;
        this.actionDo=actionDo;
        initData();
    }

    private void initData() {
//        根据id查询用户信息，添加到对话框中

        UserDao userDao = new UserDao();
        UserBean user = userDao.getUserById(id);
        txtName.setText(user.getUsername());
        txtName.setEnabled(false);
        txtPass.setText(user.getPassword());
        txtPhone.setText(user.getPhone());
        radMale.setSelected(user.getSex().equals("男"));
        radFemale.setSelected(user.getSex().equals("女"));



        btnUpdate.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name=txtName.getText();
                String phone = txtPhone.getText();
                String pass = txtPass.getText();
                String sex=radMale.isSelected()?"男":"女";

                if(!phone.matches("^1[3-9]\\d{9}$"))
                {
                    JOptionPane.showMessageDialog(null,"电话号码格式错误");
                    return;
                }
                if(!pass.matches("^\\w{6,10}$"))
                {
                    JOptionPane.showMessageDialog(null,"密码无效，请检查后重新输入");
                    return;
                }

                UserBean user = new UserBean();
                user.setId(id);
                user.setUsername(name);
                user.setPassword(pass);
                user.setPhone(phone);
                user.setSex(sex);
                 if(userDao.updateUser(user))
                 {
                     JOptionPane.showMessageDialog(null,"更新成功");
                     actionDo.afterdo(null);
                     dispose();
                 }else{
                     JOptionPane.showMessageDialog(null,"更新失败");
                 }


            }
        });
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - brightch
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        btnUpdate = new JButton();
        btnCancel = new JButton();
        txtName = new JTextField();
        txtPass = new JTextField();
        txtPhone = new JTextField();
        radMale = new JRadioButton();
        radFemale = new JRadioButton();

        //======== this ========
        setTitle("\u4fee\u6539\u7528\u6237");
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u4fee\u6539\u7528\u6237");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 18));
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(225, 35), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(45, 95), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u7535\u8bdd");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(45, 140), label3.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u5bc6\u7801");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(290, 90), label4.getPreferredSize()));

        //---- label5 ----
        label5.setText("\u6027\u522b");
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(290, 140), label5.getPreferredSize()));

        //---- btnAdd ----
        btnUpdate.setText("\u4fee\u6539");
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(new Rectangle(new Point(155, 255), btnUpdate.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(320, 255), btnCancel.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(100, 90, 150, txtName.getPreferredSize().height);
        contentPane.add(txtPass);
        txtPass.setBounds(335, 85, 180, txtPass.getPreferredSize().height);
        contentPane.add(txtPhone);
        txtPhone.setBounds(100, 135, 150, txtPhone.getPreferredSize().height);

        //---- radMale ----
        radMale.setText("\u7537");
        radMale.setSelected(true);
        contentPane.add(radMale);
        radMale.setBounds(new Rectangle(new Point(345, 140), radMale.getPreferredSize()));

        //---- radFemale ----
        radFemale.setText("\u5973");
        contentPane.add(radFemale);
        radFemale.setBounds(new Rectangle(new Point(410, 140), radFemale.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(565, 375);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - brightch
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JButton btnUpdate;
    private JButton btnCancel;
    private JTextField txtName;
    private JTextField txtPass;
    private JTextField txtPhone;
    private JRadioButton radMale;
    private JRadioButton radFemale;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
